<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\models\User;
use backend\models\GoodsClass;
use mdm\admin\components\Helper;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use backend\components\GoPager;
/* @var $this yii\web\View */
/* @var $searchModel backend\controllers\GoodsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '商品列表';
$this->params['breadcrumbs'][] = $this->title;
$this->registerCss("a.asc:after,a.desc:after{content:''};");
?>

<div class="goods-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php  echo $this->render('_search', ['model' => $searchModel,
        'allStatus'=>$allStatus,


        'classes' => $classes,
         'goodssearch'=>$goodssearch
        ]); ?>

    <p>
        <?php
        //没有创建权限不显示按钮

        if(Helper::checkRoute('create')) {
            echo Html::a('添加商品', 'javascript:edit()', ['class' => 'btn btn-success','style'=>'float:left;margin-right:10px;']);

        } ?>
        <?php

        if(Helper::checkRoute('export/in')) {

            echo Html::a('批量导入商品','#',['class'=>'btn btn-warning','data-toggle'=>'modal','data-target'=>'#myModal','style'=>'margin-right:10px;']);

        }

         ?>
<?php //echo Html::dropDownList("exportin",null,[
//                '批量导入',
//            'goods'=>'导入商品',
//            'gys'=>'导入供应商'],
//            ['class'=>'form-control',
//                'style'=>'width:25%',
//                'onchange'=>'
//                    if($(this).val()=="goods"){
//                        window.location.href="/export/goods";
//                    }else if($(this).val()=="gys"){
//                        window.location.href="/export/gys";
//                    }
//                ']);?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,

        'id'=>'grid',
        'columns' => [

            ['class' => 'yii\grid\CheckboxColumn','visible'=>Helper::checkRoute('check',Yii::$app->user->id),'headerOptions'=>['style'=>'vertical-align:top']],

            ['attribute'=>'goodsid','headerOptions'=>['style'=>'vertical-align:top']],

            ['attribute'=>'goodsno','format'=>'raw','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
               if(Helper::checkRoute('logsview',Yii::$app->user->id)){
                   return Html::a($model->goodsno,'/logs/index?gid='.$model->id);
               } else{
                   return $model->goodsno;
               }
            }],

            ['label'=>'供应商','attribute'=>'gys','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
                $gys=User::findOne($model->gys);
                return $gys ? $gys->realname : '供应商编号错误';
            }],

            ['attribute'=>'class','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
                $goodsclass=GoodsClass::findOne($model->class);
                return $goodsclass ? $goodsclass->classname : '商品分类编号错误';
            }],

            ['format'=>'raw','attribute'=>'goodsname','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
                return "<div style=\"width:100px;white-space:normal;word-break:break-all;\">".$model->goodsname."</div>";

            }
            ],

            ['label'=>'进货价','headerOptions'=>['style'=>'vertical-align:top'],'attribute'=>'inprice','value'=>function($model){

                $unit = $model->priceunit==0?"￥":"$";
                return $unit.number_format($model->inprice,2);
            }],

            ['label'=>'上次价格','headerOptions'=>['style'=>'vertical-align:top'],'attribute'=>'lastprice','value'=>function($model){
                $unit = $model->priceunit==0?"￥":"$";
                return $unit.number_format($model->lastprice,2);
            }],

            ['label'=>'上涨幅度','attribute'=>'upfee','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
                if($model->upfee>0){
                    return "+".$model->upfee.'%';
                }elseif($model->upfee==0){
                    return "0%";
                }else{
                    return $model->upfee.'%';
                }

            }],

            ['format'=>'raw','attribute'=>'remark','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
         return "<div style=\"width:100px;white-space:normal;word-break:break-all;\">".$model->remark."</div>";

            }
            ],

            ['label'=>'审核状态','attribute'=>'check','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
                if($model->check==0){
                    return '待审核';

                }elseif($model->check==1){
                    return '一审通过';
                }elseif($model->check==2){
                    return '二审通过';
                }elseif($model->check==3){
                    return '审核通过';
                }elseif($model->check==4){
                    return '审核不通过';
                }
            },'contentOptions'=>function($model){
                $u = new User();
                $myrole=$u->getmyrole();
                if(($myrole=='一级审批人' && $model->check==0) || ($myrole=='二级审批人' && $model->check==1) || ($myrole=='三级审批人' && $model->check==2)){

                    $togglemodal = [ 'data-target'=>'#checkgoods','style'=>'cursor:pointer','onclick'=>'readytocheck('.$model->id.')'];
                }else{
                    $togglemodal=[];
                }
                if($model->check==0){
                    if(array_key_exists('style',$togglemodal)){
                        $togglemodal['style'].=';color:blue';
                        return $togglemodal;
                    }
                    return array_merge($togglemodal,['style'=>'color:blue;']);


                }elseif($model->check==3){
                    return ['style'=>'color:#76C023'];
                }elseif($model->check==4){
                    return ['style'=>'color:red'];
                }else{
                    if(array_key_exists('style',$togglemodal)){
                        $togglemodal['style'].=';color:#76C023;';
                        return $togglemodal;

                    }
                    return array_merge($togglemodal,['style'=>'color:#76C023;']);

                }
            }],

            ['label'=>'一级审批人','attribute'=>'nextid','headerOptions'=>['style'=>'vertical-align:top'],'value'=>function($model){
                $user = new User();
                $yys=$user->findOne($model->nextid);

                return !empty($yys) ? $yys->realname : '未分配';
            }],
//            ['attribute'=>'export','format'=>'boolean','headerOptions'=>['style'=>'vertical-align:top']],

            ['attribute'=>'updatetime','headerOptions'=>['style'=>'vertical-align:top']],

            ['class' => 'yii\grid\ActionColumn',
                'header'=>'操作',
                'headerOptions'=>['style'=>'vertical-align:top'],
                'template'=>Helper::filterActionColumn('{progress}{update} '),
                'buttons'=>[
                     "update" => function ($url,$model)
                    {
                        $options = [
                            'title' => Yii::t('yii', 'Update'),
                            'aria-label' => Yii::t('yii', 'Update'),
                            'data-pjax' => '0',
                        ];
                        if($model->check==3 || $model->check==4){
                            return Html::a('<button class="btn btn-sm btn-primary">编辑</button>', $url, $options);
                        }else{
                            return '';
                        }


                    },
                 "progress" => function ($url,$model)
                    {
                        $options = [
                            'title' => '查看',
                            'aria-label' => Yii::t('yii', 'Update'),
                            'data-pjax' => '0',

                        ];
                        return Html::a('<button class="btn btn-sm btn-info"   >查看</button>', $url, $options);
                    },

                    ],

                'urlCreator'=>function($action, $model, $key, $index){
                    switch($action)
                    {
                        case 'progress':

                                return 'javascript:viewProgress('.$model->id.')';


                            break;

                        case 'update':
                            return 'javascript:edit(' . $model->id.')';
                            break;


                    }
                }
                ],
        ],

        'pager' =>[
            'class' => GoPager::className(),
            'firstPageLabel' => '首页',
            'prevPageLabel' => '<',
            'nextPageLabel' => '>',
            'lastPageLabel' => '尾页',
            'goPageLabel' => true,
            'totalPageLable' => '共x页',
             'goButtonLable' => 'GO',
            'maxButtonCount' => 5
]
    ]); ?>
</div>
<script>
    function checkitems(ischeck){
        var keys = $('#grid').yiiGridView('getSelectedRows');
        console.log(keys);
        if(keys==''){
            alert('请勾选之后才能批量审核！');
            return;
        }
        $.get("/goods/check","id="+keys+"&ischeck="+ischeck,function(ret){

                window.location.reload();

        });
    }
    function readytocheck(id){
        // $("#myModalLabel").html("审核商品");
        $("#checkgoods").modal({
            backdrop:'static',
            keyboard:false,
            remote: "/goods/readcheck?id="+id
        });

    }
    function viewProgress(id){
        $("#progress").modal({
            backdrop:'static',
            keyboard:false,
            remote: "/goods/progress?id="+id
        });


    }
    function edit(id=0){
        console.log(id);
        if(id==0){
            // $("#myModalLabel").html("添加商品");
            var u='/goods/create'
        }else{
            // $("#myModalLabel").html("修改商品");
            var u='/goods/update?id='+id
        }
        $("#checkgoods").modal({
            backdrop:'static',
            keyboard:false,
            remote:u
        });
    }

</script>
<?php if(Helper::checkRoute('check')) {?>
<?=Html::a('批量审核通过','javascript:checkitems(1)',['class'=>'btn btn-success']);?>

<?=Html::a('批量审核不通过','javascript:checkitems(0)',['class'=>'btn btn-warning']);?>
<?php }?>

<?php if(Helper::checkRoute('report')) {?>
    <?=Html::a('报告今天审核结果',['goods/report'],['class'=>'btn btn-primary']);?>
<?php }?>

<?php if(Helper::checkRoute('notice')) {?>
    <?=Html::a('通知相关人员今天审核结果',['goods/notice'],['class'=>'btn btn-primary']);?>
<?php }?>

<!--审批进度-->
<div class="modal fade" id="progress" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 850px;">
        <div class="modal-content" style="padding:20px">

        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!--审核商品/添加修改商品-->
<div class="modal fade" id="checkgoods" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
    <div class="modal-dialog" style="width:800px">
        <div class="modal-content" style="padding:20px">

        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!--导入excel-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<!--    <form class="form-horizontal ajaxForm2" id='formadd'  method="post" action="--><?//= Url::toRoute('/export/in')?><!--" enctype="multipart/form-data">-->
        <?php $form = ActiveForm::begin(['method'=>'post','action'=>Url::toRoute('/export/in'),'options' => ['enctype' => 'multipart/form-data']]) ?>
        <div class="modal-dialog" >
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">×
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        导入Excel
                    </h4>
                </div>

                <div class="modal-body">
                    <div class="row">
                        <div class="col-xs-12">


                            <div class="form-group">

                                <div class="col-sm-9" >
<!--                                    <input type="file" name="file"   datatype="*4-50" />-->

                                    <?= $form->field($model, 'file')->fileInput()->label("导入文件") ?>
                                    <span class="Validform_checktip"></span>

                                </div>

                            </div>
                            <div class="form-group">

                                <div class="col-sm-9" style="float:right;text-align:right;">


                                    <?= Html::a('导入模板',['/templetes/exportin.xlsx']); ?>
                                    <span class="Validform_checktip"></span>

                                </div>

                            </div>

                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" id='formbtn'  class="btn btn-primary">
                        提交保存
                    </button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        关闭
                    </button>
                    <input name="_csrf-backend" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
<!--    </form>-->
    <?php ActiveForm::end() ?>
</div>

